package com.yx.accountBook.account.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yx.accountBook.account.dao.AccountMapper;
import com.yx.accountBook.account.domain.Account;
import com.yx.accountBook.account.vo.AccountListVo;
import com.yx.accountBook.account.vo.CountVo;
import com.yx.accountBook.common.BaseService;
import com.yx.accountBook.common.BeanMapper;
import com.yx.accountBook.common.result.JsonListResult;
import com.yx.accountBook.common.result.JsonPageResult;
import com.yx.accountBook.common.result.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cuihao
 * @create 2017-11-24-13:49
 */
@Service
public class AccountService extends BaseService<Account> {
    private final static Logger log = LoggerFactory.getLogger(AccountService.class);

    @Autowired
    private AccountMapper accountMapper;

    @Resource(name = "accountMapper")
    public void setAccountMapper(AccountMapper accountMapper) {
        setMapper(accountMapper);
        this.accountMapper = accountMapper;
    }

    @Autowired
    private BeanMapper mapper;

    /**
     * 账单管理列表
     * @param type  类型: 0 全部 1：吃饭 2：油费 3：4:娱乐 5其他
     * @param minMoney 最小金额
     * @param maxMoney 最大金额
     * @param pageNo
     * @param pageSize
     * @return
     */
    public JsonPageResult getCourseList(Integer type, double minMoney, double maxMoney, int pageNo, int pageSize) {
        Example example = new Example(Account.class);
        example.setOrderByClause("id desc");
        Example.Criteria criteria = example.createCriteria();

        if (type != 0){
            criteria.andEqualTo("type", type);
        }
        if(minMoney != 0) {
            criteria.andGreaterThanOrEqualTo("money", minMoney);
        }

        if(maxMoney != 0) {
            criteria.andLessThanOrEqualTo("money", maxMoney);
        }

        Page page = PageHelper.startPage(pageNo, pageSize, true, false);
        List<Account> list = this.selectByExample(example);

        List<AccountListVo> listVos = new ArrayList<>();
        for(Account account : list){
            AccountListVo vo = mapper.map(account,AccountListVo.class);
            if(account.getType()==1){
                vo.setTypeName("吃饭");
            }else if(account.getType()==2){
                vo.setTypeName("油费");
            }else if(account.getType()==3){
                vo.setTypeName("上交");
            }else if(account.getType()==4){
                vo.setTypeName("娱乐");
            }else if(account.getType()==5){
                vo.setTypeName("其他");
            }
            listVos.add(vo);
        }

        return new JsonPageResult(listVos, page);
    }


    /**
     * 删除账单
     * @param id
     * @return
     */
    public JsonResult deleteById(long id) {
        accountMapper.deleteByPrimaryKey(id);
        return new JsonResult(true, "删除成功");
    }

    /**
     * 获取账单明细
     * @param id
     * @return
     */
    public JsonResult getDetailById(long id) {
        Account account = accountMapper.selectByPrimaryKey(id);
        return new JsonResult(true,"",account);
    }

    /**
     * 新增账单
     * @param account
     * @return
     */
    @Transactional
    public JsonResult addAccount(Account account) {
//        long userId = CurrentUser.getUser().getId();
//        account.setUserId(userId);
        account.setUserId(1l);
        accountMapper.insertSelective(account);
        return new JsonResult(true, "新增成功",account.getId());
    }

    /**
     * 修改账单
     * @param account
     * @return
     */
    @Transactional
    public JsonResult editAccount(Account account) {
        accountMapper.updateByPrimaryKeySelective(account);
        return new JsonResult(true, "修改成功");
    }

    /**
     * 统计账单
     * @param
     * @return
     */
    public JsonListResult count(){

        List<CountVo> list = accountMapper.count();
        for(CountVo vo : list){
            int type = vo.getType();//类型 1：吃饭 2：油费 3：上交 4:娱乐 5其他
            switch (type){
                case 1:
                    vo.setTypeName("吃饭");
                    break;
                case 2:
                    vo.setTypeName("油费");
                    break;
                case 3:
                    vo.setTypeName("上交");
                    break;
                case 4:
                    vo.setTypeName("娱乐");
                    break;
                case 5:
                    vo.setTypeName("其他");
                    break;
            }

        }

        return new JsonListResult(true,"",list);
    }

}
